package com.pcbly.web.action.system;
 
import java.util.HashMap; 
import java.util.List; 

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.field.FormSortBean;
import com.pcbly.web.bean.field.FormTypeBean; 
import com.pcbly.web.business.form.FormSortService;
import com.pcbly.web.business.form.FormTypeService;
import com.pcbly.web.business.form.MacroComponentService;
import com.pcbly.web.business.table.TableService;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.components.form.AnalyseRequestData;

public class FormSortAction extends BaseAction<FormSortBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4997009141304744333L;

	protected FormSortService formSortService;

	protected FormTypeService formTypeService;

	private String dataId; 

	private TableService tableService;
	
	private MacroComponentService macroComponentService;

	private List<FormSortBean> formSortBeans;

	private List<FormTypeBean> formTypeBeans;

	private FormSortBean formSortBean;

	private FormTypeBean formTypeBean;

	private String toDisplayData;

	private String formId; 

	public String getFormId() {
		return formId;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getToDisplayData() {
		return toDisplayData;
	}

	public void setToDisplayData(String toDisplayData) {
		this.toDisplayData = toDisplayData;
	}

	public FormSortBean getFormSortBean() {
		return formSortBean;
	}

	public void setFormSortBean(FormSortBean formSortBean) {
		this.formSortBean = formSortBean;
	}

	public FormTypeBean getFormTypeBean() {
		return formTypeBean;
	}

	public void setFormTypeBean(FormTypeBean formTypeBean) {
		this.formTypeBean = formTypeBean;
	}

	public List<FormSortBean> getFormSortBeans() {
		return formSortBeans;
	}

	public void setFormSortBeans(List<FormSortBean> formSortBeans) {
		this.formSortBeans = formSortBeans;
	}

	public List<FormTypeBean> getFormTypeBeans() {
		return formTypeBeans;
	}

	public void setFormTypeBeans(List<FormTypeBean> formTypeBeans) {
		this.formTypeBeans = formTypeBeans;
	}

	public void setFormSortService(FormSortService formSortService) {
		this.formSortService = formSortService;
	}

	 

	public String dwrSave(String data, String formId,String dataId) throws Exception {
		super.dwrBaseSave("");
		if (data == null || "".equals(data.trim())) {
			return "error";
		} else {
			AnalyseRequestData requestData=new AnalyseRequestData();
			requestData.setFormTableId(dataId);
			requestData.setData(data);
			requestData.setFormId(formId);
			requestData.setTableService(tableService);
			HashMap<String, String> map=requestData.analyseData();
			if(map==null){
				return "error";
			}
			macroComponentService.saveDataHashMap(map);
		}
		return "ok";
	}

	public String view() throws Exception { 
		if (getId() == null) {
			setId("4028ffbc252f990d01252fa910860012");
		}
		if(dataId==null){
			dataId=(new GUID()).toString();
		}
		toDisplayData = formTypeService.getStringBByFormTypeId(getId(),dataId).toString();
		return super.view();
	}

	public String show() throws Exception {
		
		return super.view();
	}

	public String modify() throws Exception {

		return super.edit();
	}

	public String deleteFormType(String id) throws Exception {
		try {
			formTypeService.deleteFormType(id);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return "error";
		}
	}

	public String showFormType() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("showFormSort login.........");
		}
		formTypeBean = formTypeService.findFormTypeBeanById(getId());
		return super.view();
	}

	public String showFormSort() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("showFormType login.........");
		}
		formSortBeans = formSortService.findFormSortBeans();

		return super.view();
	}

	public String saveFormType(FormTypeBean bean) throws Exception {
		try {

			if (log.isDebugEnabled()) {
				log.debug("saveFormType login.........");
				log.debug(bean.getId());
				log.debug(bean.getDepartId());
				log.debug(bean.getFormName());
				log.debug(bean.getPrintModel());
				log.debug("before .........");
			}
			if (bean.getId() == null || "".equals(bean.getId())) {
				GUID guId = new GUID();
				bean.setId(guId.toString());
				formTypeService.saveFormType(bean);
			} else {
				formTypeService.updateFormType(bean);
			}
			if (log.isDebugEnabled()) {
				log.debug("after .........");
				log.debug(bean.getId());
			}
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return "error";
		}
	}

	public void setFormTypeService(FormTypeService formTypeService) {
		this.formTypeService = formTypeService;
	}

	public List<FormTypeBean> getFormTypesByFormSortId(String formSortId)
			throws Exception {
		return formTypeService.findFormTypeBeansBySortId(formSortId);
	}

	public FormTypeBean getFormTypeById(String Id) throws Exception {
		return formTypeService.findFormTypeBeanById(Id);
	}

	public String list() throws Exception {
		formSortBeans = formSortService.findFormSortBeans();
		return SUCCESS;
	}

	// public void setColumnService(ColumnService columnService) {
	// this.columnService = columnService;
	// }

	public void setTableService(TableService tableService) {
		this.tableService = tableService;
	}

	public void setMacroComponentService(MacroComponentService macroComponentService) {
		this.macroComponentService = macroComponentService;
	}
}
